package com.example.edwincompose.feature.splash

import android.window.SplashScreen
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.edwincompose.R
import com.example.edwincompose.util.SuperDataUtil

@Composable
fun SplashRoute(
    toMain: () -> Unit = {},
    viewModel: SplashViewModel = viewModel()

) {
    //即使 Composable 函数重组多次，收集器也只会在生命周期变化时重新连接  不会重新创建 ViewModel (因为 viewModel() 使用 remember 机制)
    val timeLeft by viewModel.timeLeft.collectAsStateWithLifecycle()//不会重复创建？
    val navigateToMain by viewModel.navigateToMain.collectAsStateWithLifecycle()//不会重复创建？

    SplashScreen(
        year = SuperDataUtil.currentYear(),
        timeLeft=timeLeft,//这种是什么意思？？？
        toMain = viewModel::returnSplash
    )

    if (navigateToMain){
        LaunchedEffect(Unit) {
            toMain.invoke()
        }
    }

}

@Composable
fun SplashScreen(
    year: Int = 2024,
    timeLeft:Long = 0,
    toMain: () -> Unit = {},
) {
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.primary)
    ) {
        Image(
            painter = painterResource(R.drawable.ic_launcher_foreground),
            contentDescription = null,
            modifier = Modifier
                .padding(top = 70.dp)
                // 在 Box 布局中：默认约束​​：当直接使用 align() 修饰符时，Box 会为子项提供​​最大可用尺寸​​作为约束
                .align(Alignment.TopCenter)//为什么这样就居中了呢？是占满了整一行的宽度？
                .clickable {
                    //如何从引导页面跳转到首页呢？通过回调的方式
                    toMain.invoke()

                }
        )

        Image(
            painter = painterResource(R.drawable.ic_launcher_foreground),
            contentDescription = null,
            modifier = Modifier
                .padding(bottom = 70.dp)
                // 在 Box 布局中：默认约束​​：当直接使用 align() 修饰符时，Box 会为子项提供​​最大可用尺寸​​作为约束
                .align(Alignment.BottomCenter)//为什么这样就居中了呢？是占满了整一行的宽度？
        )
        Text(
            stringResource(R.string.kcopyright, year),
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(bottom = 30.dp),
            color = Color.White,
            style = TextStyle(
                fontSize = 12.sp
            )
        )
        Text(
            text = "倒计时:$timeLeft",
            color = Color.White,
            modifier = Modifier.align(Alignment.TopEnd).padding(top = 10.dp, end = 10.dp)
        )
    }

}

@Preview(showBackground = true)
@Composable
fun SplashRouteView() {

    SplashRoute()
}